While Loop

A while loop generally takes a condition in parenthesis. If the condition is True then the code within the body of the while loop is executed. A while loop is used when we don’t know the number of times we want the loop to be executed however we know the termination condition of the loop. It is also known as an entry controlled loop as the condition is checked before executing the loop. The while loop can be thought of as a repeating if statement.

while (condition)
{
    // Code to be executed
}

While loop starts with the checking of the condition. If it evaluated to true, then the loop body statements are executed otherwise first statement following the loop is executed. For this reason, it is also called Entry control loop. Once the condition is evaluated to true, the statements in the loop body are executed. Normally the statements contain an update value for the variable being processed for the next iteration.When the condition becomes false, the loop terminates which marks the end of its life cycle.
object Demo
{
def main(args: Array[String])
{
var x = 1;
while (x <= 4)
{
println("Value of x: " + x);
x = x + 1;
}
}
}

Value of x: 1
Value of x: 2
Value of x: 3
Value of x: 4

Infinite While Loop: While loop can execute infinite times which means there is no terminating condition for this loop. In other words, we can say there are some conditions which always remain true, which causes while loop to execute infinite times or we can say it never terminates.

Example: Below program will print the specified statement infinite time and also give the runtime error Killed (SIGKILL) on online IDE.

object Demo
{
    def main(args: Array[String])
    {
        var x = 1;
        while (x < 5)
        {
            println("Hello Scala")
        }
    }
}

do..while Loop
A do..while loop is almost same as a while loop. The only difference is that do..while loop runs at least one time. The condition is checked after the first execution. A do..while loop is used when we want the loop to run at least one time. It is also known as the exit controlled loop as the condition is checked after executing the loop.

do {

// statements to be Executed

} while(condition);

// Scala program to illustrate do..while loop
object dowhileLoopDemo
{
     // Main method
    def main(args: Array[String])
    {
        var a = 10;

        // using do..while loop
        do
        {
            print(a + " ");
            a = a - 1;
        }while(a > 0);
    }
}

Nested Loops
The loop which contains a loop inside a loop is known as the nested loop. It can contain the for loop inside a for loop or a while loop inside a while loop. It is also possible that a while loop can contain the for loop and vice-versa.

// Scala program to illustrate nested loop
object nestedLoopDemo {

  // Main Method
  def main(args: Array[String]) {

    var a = 5;
    var b = 0;

    // outer while loop
    while (a < 7)
    {
       b = 0;
  
       // inner while loop
       while (b < 7 )
       {
      
           // printing the values of a and b
          println("Value of a = " +a, " b = "+b);
          b = b + 1;
       }
  
       // new line
       println()
  
       // incrementing the value of a
       a = a + 1;
  
       // dispalying the updated value of a
       println("Value of a Become: "+a);
  
       // new line
       println()
    }

  }
}
Output:

(Value of a = 5, b = 0)
(Value of a = 5, b = 1)
(Value of a = 5, b = 2)
(Value of a = 5, b = 3)
(Value of a = 5, b = 4)
(Value of a = 5, b = 5)
(Value of a = 5, b = 6)

Value of a Become: 6

(Value of a = 6, b = 0)
(Value of a = 6, b = 1)
(Value of a = 6, b = 2)
(Value of a = 6, b = 3)
(Value of a = 6, b = 4)
(Value of a = 6, b = 5)
(Value of a = 6, b = 6)

Value of a Become: 7


A pattern match includes a sequence of alternatives, each starting with the keyword case. Each alternative includes a pattern and one or more expressions, which will be evaluated if the pattern matches. An arrow symbol => separates the pattern from the expressions.

object ScalaDemoApp {
   def main(args: Array[String]) {
      println(matchTest(3))
   }

   def matchTest(x: Int): String = x match {
      case 1 => "one"
      case 2 => "two"
      case _ => "many"
   }
}

object ScalaDemoApp {
    def main(args: Array[String]) {
          println(matchTest("two"))
          println(matchTest("test"))
          println(matchTest(1))
       }
    
       def matchTest(x: Any): Any = x match {
          case 1 => "one"
          case "two" => 2
          case y: Int => "scala.Int"
          case _ => "many"
       }
    }

No comments:

Post a Comment